package com.neusoft.elm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.service.FoodService;
import com.neusoft.elm.service.impl.FoodServiceImpl;

@WebServlet("/GetFoodListServlet")
public class GetFoodListServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        
        PrintWriter out = response.getWriter();
        
        try {
            String businessIdStr = request.getParameter("businessId");
            if (businessIdStr == null || businessIdStr.trim().isEmpty()) {
                out.print("{\"success\":false,\"message\":\"商家ID不能为空\"}");
                return;
            }
            
            Integer businessId = Integer.valueOf(businessIdStr);
            
            FoodService foodService = new FoodServiceImpl();
            List<Food> foodList = foodService.listFoodByBusinessId(businessId);
            
            Gson gson = new Gson();
            String jsonResult = gson.toJson(foodList);
            
            out.print(jsonResult);
            
        } catch (Exception e) {
            e.printStackTrace();
            out.print("{\"success\":false,\"message\":\"获取食品列表失败: " + e.getMessage() + "\"}");
        } finally {
            out.close();
        }
    }
}